﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Reflection;
using System.ComponentModel;
using System.Windows.Forms;

namespace TLL.UTL
{
    public enum RoundType : int 
    {
        ToHigher,
        ToLowest
    }
    public static class Utilities
    {
        public static int ParseToInt(double Item)
        {
            return int.Parse(Item.ToString().Split(new char[] {'.',','})[0]);
        }
        public static bool SaveToLogs(string Url, string NewUrl)
        {
            try { File.Copy(Url, NewUrl, false); }
            catch (Exception ex) { return false; }
            return true;
        }
        public static int GetDayCount(DateTime Date, DateTime OutDate)
        {
            int res = 0;
            while (Date < OutDate)
            {
                Date = Date.AddDays(1);
                if (Date.DayOfWeek != DayOfWeek.Sunday && Date.DayOfWeek != DayOfWeek.Saturday)
                    res++;
            }
            return res;
        }
        public static bool CreateNewXmlDataLogFile(string URL)
        {
            try
            {
                using (StreamWriter writer = new StreamWriter(URL, false, Encoding.UTF8))
                    writer.Write(@"<?xml version='1.0' encoding='utf-8'?>
                                 <Items></Items>");
            }
            catch { return false; }
            return true;
        }
        public static bool DeleteXmlDataLogFile(string URL)
        {
            try { File.Delete(URL); }
            catch { return false; }
            return true;
        }
        public static T GetEnum<T>(string name)
        {
            try
            {
                foreach (var value in Enum.GetValues(typeof(T)))
                    if (((Enum)value).ToString() == (name == "RUR" ? "RUB" : name))
                        return (T)value;
            }
            catch { }
            return default(T);
        }
        public static void RemoveClickEvent(NotifyIcon b)
        {
            FieldInfo f1 = typeof(Control).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
            object obj = f1.GetValue(b);
            PropertyInfo pi = b.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);
            EventHandlerList list = (EventHandlerList)pi.GetValue(b, null);
            list.RemoveHandler(obj, list[obj]);
        }
        public static int Round(double number, RoundType type)
        {
            switch (type)
            {
                case RoundType.ToLowest: return (int)Math.Round(number);
                case RoundType.ToHigher:
                    var line = number.ToString().Split('.', ',');
                    var res = int.Parse(line[0]);
                    var secondNum = line[1].Replace("0", "");
                    return int.Parse(secondNum.Length > 5 ? secondNum.Substring(0 , 5) : secondNum) != 0 ? res + 1 : res;
            }
            return 0;
        }
        public static string SendXMLRequest(string url, string body)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(body);
            request.ContentType = "text/xml; encoding='utf-8'";
            request.ContentLength = bytes.Length;
            request.Method = "POST";
            //request.Proxy = new WebProxy("1.160.155.38", 80);
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream responseStream = response.GetResponseStream();
                string responseStr = new StreamReader(responseStream).ReadToEnd();
                return responseStr;
            }
            return null;
        }
        public static string GetSHA1(string input)
        {
            var sh1 = new SHA1CryptoServiceProvider();
            return sh1.ComputeHash(Encoding.UTF8.GetBytes(input)).Select(b => b.ToString("x2")).Aggregate("", (total, cur) => total + cur);
        }
        public static string MD5Hash(string line)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            return md5.ComputeHash(Encoding.ASCII.GetBytes(line)).Select(b => b.ToString("x2")).Aggregate("", (total, cur) => total + cur);
        }
    }
}
